package cn.newtouch._07_tran;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.Date;

public class Producer {

    public static void main(String[] args) throws Exception {
        //1.获取生产者对象
        //DefaultMQProducer producer = new DefaultMQProducer("tran_producer_group");
        TransactionMQProducer producer = new TransactionMQProducer("tran_producer_group");
        //2.设置namesever地址
        producer.setNamesrvAddr("localhost:9876");
        //3.启动
        //设置监听
        producer.setTransactionListener(new MyTranListener());
        producer.start();
        //4.生成消息并发送
        Message message = new Message("tran", "hello", "天天写bug!!好开心！！".getBytes(StandardCharsets.UTF_8));
        //延迟级别 3，代表 10s延迟
        message.setDelayTimeLevel(3);

        //发送方式变为监听发送send-->sendMessageInTransaction
        SendResult send = producer.sendMessageInTransaction(message,"监听事务成功！！");

        System.out.println(new Date().toLocaleString());
        System.out.println(send);
        //5.关闭
        producer.shutdown();
    }
}
